package com.zeny.edu.ucenter.controller;


import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.ucenter.entity.Member;
import com.zeny.edu.ucenter.service.MemberService;
import com.zeny.edu.ucenter.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zeny
 * @since 2020-03-08
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * @Description 根据token解析出用户信息
     * @Date 2020/3/11 0011 23:27
     * @param token
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/userInfo/{token}")
    public ResponseResult getUserInfo(@PathVariable(required = false) String token) {
        if (StringUtils.isNotBlank(token) && JwtUtils.checkJwt(token) != null) {
            Claims claims = JwtUtils.checkJwt(token);
            String nickname = (String) claims.get("nickname");
            String avatar = (String) claims.get("avatar");
            String id = (String) claims.get("id");
            Member member = new Member();
            member.setId(id);
            member.setNickname(nickname);
            member.setAvatar(avatar);
            return ResponseResult.ok().data("member", member);
        }else {
            return ResponseResult.fail();
        }
    }

    /**
     * @Description 今日注册数
     * @Date 2020/3/8 0008 16:36
     * @param day 
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/countRegister/{day}")
    public ResponseResult registerCount(@PathVariable String day) {
        Integer count = memberService.countRegisterByDay(day);
        return ResponseResult.ok().data("count", count);
    }

}

